package com.markerhub.controller;


import com.markerhub.common.dto.ClassStudentUpdateDto;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.ManagerStudent;
import com.markerhub.service.ManagerStudentService;
import com.markerhub.service.TeachingStudentInClassService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teaching-student-in-class")
public class TeachingStudentInClassController {
    @Autowired
    TeachingStudentInClassService studentInClassService;
    @Autowired
    ManagerStudentService studentService;
 /*   @GetMapping("/{classId}")
    public Result getStudentsByClassId(@PathVariable("classId") int classId) {
        try {
            // 调用服务层方法获取学生姓名列表
            List<Integer> studentIds = studentInClassService.getStudentIdByClassId(classId);

            // 2. 获取学生详细信息（根据学生ID列表）
            List<ManagerStudent> students = studentService.getByIds(studentIds);

            // 3. 返回包含学生信息的结果
            return Result.succ(students);

    }*/

    @RequiresAuthentication
    @PostMapping("/update-students")
    public Result updateClassStudents(@RequestBody ClassStudentUpdateDto classStudentUpdateDto) {
        log.info(String.valueOf(classStudentUpdateDto));
        try {
            studentInClassService.updateClassStudents(classStudentUpdateDto.getId(), classStudentUpdateDto.getStudentIds());
            return Result.succ("学员信息更新成功");
        } catch (Exception e) {
            return Result.fail("保存学员信息失败：" + e.getMessage());

        }
    }

    @GetMapping("/{classId}")
    public Result getStudentsByClassId(@PathVariable("classId") int classId) {

            // 调用服务层方法获取学生姓名列表
            List<Integer> studentIds = studentInClassService.getStudentIdByClassId(classId);

            // 2. 获取学生详细信息（根据学生ID列表）
            List<ManagerStudent> students = studentService.getByIds(studentIds);

            // 3. 返回包含学生信息的结果
            return Result.succ(students);


    }
}